package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.HisBedDto;
import com.woniu.dto.PageDTO;
import com.woniu.exception.DIYException;
import com.woniu.model.HisBed;
import com.woniu.mapper.HisBedMapper;
import com.woniu.param.HisBedAddParam;
import com.woniu.param.HisBedParam;
import com.woniu.param.PageParam;
import com.woniu.service.IHisBedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.CopyBeanUtil;
import com.woniu.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-11
 */
@Service
public class HisBedServiceImpl extends ServiceImpl<HisBedMapper, HisBed> implements IHisBedService {
    @Resource
    private HisBedMapper hisBedMapper;


    @Override
    public Result<PageDTO<HisBedDto>> getAllHisBed() {
        return null;
    }

    @Override
    public Result<PageDTO<HisBedDto>> getHisBedBy(HisBedParam hisBedParam) {
        if (hisBedParam==null){new DIYException(-9999,"hisBedParam为空");}
        QueryWrapper<HisBed> queryWrapper = new QueryWrapper();
        if (hisBedParam.getBedId()!=null){
            queryWrapper.eq("bed_id",hisBedParam.getBedId());
        }
        if (hisBedParam.getBedNum()!=null){
            queryWrapper.eq("bed_num",hisBedParam.getBedNum());
        }
        if (hisBedParam.getRoomId()!=null){
            queryWrapper.eq("room_id",hisBedParam.getRoomId());
        }
        if (hisBedParam.getRoomName()!=null){
            queryWrapper.eq("room_name",hisBedParam.getRoomName());
        }
        if (hisBedParam.getOfficeId()!=null){
            queryWrapper.eq("office_id",hisBedParam.getOfficeId());
        }
        if (hisBedParam.getNurseId()!=null){
            queryWrapper.eq("nurse_id",hisBedParam.getNurseId());
        }
        if (hisBedParam.getNurseName()!=null){
            queryWrapper.eq("nurse_name",hisBedParam.getNurseName());
        }
        if (hisBedParam.getBedIsuse()!=null){
            queryWrapper.eq("bed_isuse",hisBedParam.getBedIsuse());
        }
        if(hisBedParam.getPageParam()==null){
            PageParam pageParam = new PageParam();
            pageParam.setPage(0L);
            pageParam.setPageSize(100L);
            hisBedParam.setPageParam(pageParam);
        }
        Page<HisBed> page = page(new Page<HisBed>(hisBedParam.getPageParam().getPage(), hisBedParam.getPageParam().getPageSize()), queryWrapper);
        List<HisBed> records = page.getRecords();
        System.out.println(records);
        List<HisBedDto> hisBedDtos = CopyBeanUtil.copyBean(records, HisBedDto.class);
        return Result.bulid(PageDTO.bulid(page,hisBedDtos));
    }

    @Override
    @PostMapping("addHisBed")
    public Result<HisBedDto> addHisBed(HisBedAddParam hisBedAddParam) {
        if (hisBedAddParam==null)new DIYException(-9999,"hisBedAddParam为空");
        HisBed hisBed = new HisBed();
        BeanUtils.copyProperties(hisBedAddParam,hisBed);
        hisBedMapper.insert(hisBed);
        HisBedDto hisBedDto = new HisBedDto();
        BeanUtils.copyProperties(hisBed,hisBedDto);
        return Result.bulid(hisBedDto);
    }

    @Override
    public Result<HisBedDto> updateHisBed(HisBedParam hisBedParam) {
        if(hisBedParam==null){new DIYException(-9999,"床位修改数据为空");}
        HisBed hisBed = new HisBed();
        BeanUtils.copyProperties(hisBedParam,hisBed);
        hisBedMapper.updateById(hisBed);
        HisBedDto hisBedDto = new HisBedDto();
        BeanUtils.copyProperties(hisBed,hisBedDto);
        return Result.bulid(hisBedDto);
    }

    @Override
    public Result<Boolean> deleteHisBedById(ArrayList<Integer> id) {
        if (id==null) new DIYException(-9999,"服务器异常，id==null");
        int i = hisBedMapper.deleteBatchIds(id);
        System.out.println(i);
        return Result.bulid(i==0?new Boolean(false):new Boolean(true));
    }

}
